#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

# Get dependencies
"${DIRECTORY}/pkg-install" "libsquashfuse0 squashfuse fuse libgconf-2-4" "$(dirname "$0")" || error "Failed to install libsquashfuse0 squashfuse fuse"
sudo apt install -y snapd || error "APT failed to install snapd"

sudo systemctl enable --now snapd.socket || error "Unable to enable snapd.socket"
sudo systemctl start snapd.service
if [ $? != 0 ];then
  echo "Unable to start snapd.service. Running debug..."
  echo "Running 'systemctl status snapd.service'"
  systemctl status snapd.service
  exit 1
fi

sudo snap refresh
sudo systemctl restart snapd.service || error "Unable to restart snapd.service"

sudo modprobe squashfs || exit 1

sudo snap install core18 || error "Snapd was unable to install core18"
sudo snap install core || error "Snapd was unable to install core"
sudo snap install snapd || error "Snapd was unable to install snapd"

sudo ln -s /var/lib/snapd/snap /snap

echo "Output of 'snap version':"
snap version

sudo snap install snap-store || error "Unable to install snap store"
sudo rm -rf /usr/share/applications/snap
ln -s /var/lib/snapd/desktop/applications /usr/share/applications/snap || sudo ln -s /var/lib/snapd/desktop/applications /usr/share/applications/snap || error "Failed to create symlink for Snap app shortcuts!"


